/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project1;

import java.util.HashMap;

/**
 *
 * @author rr
 */
public class TemporalRequirementsConstraint implements Constraint {

    @Override
    public boolean check(int[] assignments, int variableIndex, int valueIndex) {


        int[][] reqs = SetUp.getInstance().getTemporalRequirementsMatrix();
        int j = variableIndex % SetUp.getInstance().getLengthOfSchedule(); //weekday of new assignment

        //check for max
        int i = valueIndex;
        if (i < reqs.length) { //off shift is not checked            
            int employeeCountNeeded = reqs[i][j];

            int employeeSum = 1; //new assignment

            for (int emp = 0; emp < SetUp.getInstance().getNumberOfEmployees(); ++emp) {
                int index = emp * SetUp.getInstance().getLengthOfSchedule() + j;
                if (assignments[index] == i) {
                    ++employeeSum;
                }
            }
            if (employeeSum > employeeCountNeeded) {
                return false;
            }
        }

        //check for min
        for (i = 0; i < SetUp.getInstance().getNumberOfShifts(); i++) {
            int employeeCountNeeded = reqs[i][j];
            if (i != valueIndex) { //for all other shifts
                int employeeSum = SetUp.getInstance().getNumberOfEmployees();

                for (int emp = 0; emp < SetUp.getInstance().getNumberOfEmployees(); ++emp) {
                    int index = emp * SetUp.getInstance().getLengthOfSchedule() + j;
                    if (assignments[index]!=-1 && assignments[index] != i) {
                        --employeeSum;
                    }
                }

                if (employeeSum < employeeCountNeeded) {
                    return false;
                }
            }
        }


        return true;
    }
}
